<template>
  <table-panel title="实际回款">
    <div slot="btm">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        @click="handleAdd"
        v-hasPermi="['crm:receivables:add']"
      >新增</el-button>
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        @click="handleExport"
        v-hasPermi="['crm:receivables:export']"
      >导出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <div slot="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="120px">
        <el-form-item label="回款方式：" prop="returnType">
          <el-select
            v-model="queryParams.returnType"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="dict in dict.type.receivables_plan_returntype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回款单状态：" prop="checkStatus">
          <el-select
            v-model="queryParams.checkStatus"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="dict in dict.type.receivables_check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div slot="content">
      <el-table v-loading="loading" :height="tableHeight" border :data="receivablesList" @selection-change="handleSelectionChange">
        <div slot="empty"><st-empty/></div>
        <el-table-column label="回款单" align="center" prop="receivablesId" >
          <template slot-scope="scope">
            <el-button type="text" @click="showReceivables(scope.row)">{{scope.row.receivablesId}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="回款计划" align="center" prop="planId" >
          <template slot-scope="scope">
            <el-button type="text" @click="showReceivablesPlan(scope.row)">{{scope.row.planId}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="关联合同" align="center" prop="contractName" >
          <template slot-scope="scope">
            <el-button type="text" @click="showContract(scope.row)">{{scope.row.contractName}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="关联客户" align="center" prop="customerName" >
          <template slot-scope="scope">
            <el-button type="text" @click="showCustomer(scope.row)">{{scope.row.customerName}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="回款日期" align="center" prop="returnTime" />
        <el-table-column label="回款方式" align="center" prop="returnTypeDisp" />
        <el-table-column label="回款金额" align="center" prop="money" />
        <el-table-column label="状态" align="center" prop="checkStatusDisp" />
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['crm:receivables:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['crm:receivables:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <customer-detail v-if="customerDetailFlag" :params="customerForm" ref="customerDetailRef" @close="customerDetailFlag=false"></customer-detail>
      <contract-detail v-if="contractDetailFlag" :params="contractForm" ref="contractDetailRef" @close="contractDetailFlag=false"></contract-detail>
      <plan-detail v-if="planDetailFlag" :params="planForm" ref="planDetailRef" @close="planDetailFlag=false" ></plan-detail>
      <receivables-detail v-if="receivablesDetailFlag" :params="receivablesForm" ref="receivablesDetailRef" @close="receivablesDetailFlag=false" ></receivables-detail>
    </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </table-panel>
</template>

<script>
import { listReceivables, getReceivables, delReceivables, addReceivables, updateReceivables } from "@/api/crm/receivables";
import contractDetail from '@/views/crm/contract/detail/index.vue';
import customerDetail from '@/views/crm/customer/detail/index.vue';
import planDetail from '@/views/crm/plan/detail/index.vue';
import receivablesDetail from './detail/index.vue';
export default {
  name: "Receivables",
  dicts:['receivables_plan_returntype','receivables_check_status'],
  components:{
    customerDetail,
    contractDetail,
    planDetail,
    receivablesDetail
  },
  props:{
    customerForm:{
      type:Object,
      default:function() {
        return {};
      }
    }
  },
  data() {
    return {
      customerDetailFlag:false,
      contractDetailFlag:false,
      planDetailFlag:false,
      receivablesDetailFlag:false,
      customerForm: {},
      contractForm:{},
      planForm:{},
      receivablesForm:{},
      tableHeight:undefined,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回款表格数据
      receivablesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnType:'',
        checkStatus:'',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询回款列表 */
    getList() {
      this.loading = true;
      listReceivables(this.queryParams).then(response => {
        this.receivablesList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(()=>{
          this.tableHeight=this.getTableHeightWithTitle();
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        receivablesId: null,
        number: null,
        planId: null,
        customerId: null,
        contractId: null,
        returnTime: null,
        returnType: null,
        money: null,
        remark: null,
        checkStatus: null,
        examineRecordId: null,
        createdBy: null,
        ownerUserId: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    receivablesPlanSelect(){

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.receivablesId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    showReceivablesPlan(row){
      this.planDetailFlag=true;
      this.planForm=row;
      this.$nextTick(()=>{
        this.$refs.planDetailRef.show();
      })
    },
    showCustomer(row){
      this.customerForm=row;
      this.customerDetailFlag=true;
      this.$nextTick(() => {
        this.$refs.customerDetailRef.show();
      })
    },
    showContract(row){
      this.contractDetailFlag=true;
      this.contractForm=row;
      this.$nextTick(()=>{
        this.$refs.contractDetailRef.show();
      })
    },
    showReceivables(row){
      this.receivablesDetailFlag=true;
      this.receivablesForm=row;
      this.$nextTick(()=>{
        this.$refs.receivablesDetailRef.show();
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      /*this.reset();
      this.open = true;
      this.title = "添加回款";*/
      this.$emit('add');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$emit('modify',row);
      /*this.reset();
      const receivablesId = row.receivablesId || this.ids
      getReceivables(receivablesId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改回款";
      });*/
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.receivablesId != null) {
            updateReceivables(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceivables(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const receivablesIds = row.receivablesId || this.ids;
      this.$modal.confirm('是否确认删除回款编号为"' + receivablesIds + '"的数据项？').then(function() {
        return delReceivables(receivablesIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/receivables/export', {
        ...this.queryParams
      }, `receivables_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
.el-form{
  text-align: center;
}
.el-select {
  width : 100%;
}
.el-cascader{
  width: 100%;
}
.el-date-editor{
  width: 100%;
}
::v-deep .el-tabs--border-card > .el-tabs__content{
  padding: 0px !important;
}
</style>
